import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const state = {
  isShowPlayer: false,
  playerState: 0,//0-静止，1-播放，2-暂停
  showTabs: false,
  songs: [],
  songsHistory: [],
  playNowTime:0,
}

const mutations = {
  changeShowPlayer(state) {
    state.isShowPlayer = !state.isShowPlayer
  },
  changePlayerState(state, payload) {
    state.playerState = payload.playerState;
  },
  changeHeaderShow(state, payload) {
    state.showTabs = payload.showTabs;
  },
  addSongs(state, payload) {
    state.songs = state.songs.filter(a => { return payload.songs.indexOf(a) == -1 });
    state.songs.splice(0, 0, ...payload.songs);
    state.playerState = 1;
  },
  deleteSongs(state, payload) {
    let song = state.songs.shift();
    //将旧的已存在的历史记录删除
    state.songsHistory = state.songsHistory.filter(a => { return a.songmid != song });
    //将新的歌曲压入栈
    state.songsHistory.push(song)
  },
  addSongsHistory(state, payload) {
    //将旧的已存在的历史记录删除
    state.songsHistory = state.songsHistory.filter(a => { return a.songmid != payload.song.songmid });
    //将新的歌曲压入栈
    state.songsHistory.push(payload.song)
  },
  deleteSongsHistory(state, payload) {
    //涉及到报错异常todo
    let song = state.songsHistory.pop();
    state.songs.splice(0, 0, song);
    state.playerState = 1;
  },
  changePlayNowTime(state, payload) {
    state.playNowTime=payload.playNowTime;
  },
}

export default new Vuex.Store({
  state,
  mutations,
  actions: {
  },
  modules: {
  }
})
